﻿using System;
using System.Linq;

namespace DesignByFarooq.PowershellByExample
{
    public sealed class MemoryStatusFilePartition
    {
        public int StartOffset { get; private set; }
        public int EndOffset { get; private set; }

        public MemoryStatusFilePartition(
            int startOffset
            )
            :this(startOffset, 0)
        {
        }

        public MemoryStatusFilePartition(
            int startOffset,
            int endOffset
            )
        {
            if (startOffset < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "startOffset",
                    startOffset,
                    "StartOffset must be zero or greater."
                    );
            }

            if (endOffset < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "endOffset",
                    startOffset,
                    "EndOffset must be zero or greater."
                    );
            }

            if ((endOffset != 0) && (startOffset > endOffset))
            {
                throw new InvalidOperationException(
                    "StartOffset cannot be greater than EndOffset unless EndOffset is zero."
                    );
            }

            this.StartOffset = startOffset;
            this.EndOffset = endOffset;
        }

        public override bool Equals(
            object obj
            )
        {
            if (obj == null)
            {
                return false;
            }

            if (object.ReferenceEquals(this, obj) != false)
            {
                return true;
            }

            if (this.GetType() != obj.GetType())
            {
                return false;
            }

            MemoryStatusFilePartition partition = obj as MemoryStatusFilePartition;

            return this.Equals(
                partition
                );
        }

        private bool Equals(
            MemoryStatusFilePartition partition
            )
        {
            return (this.StartOffset == partition.StartOffset) &&
                (this.EndOffset == partition.EndOffset);
        }
    }
}
